import org.w3c.dom.ls.LSException;
import sun.security.krb5.internal.LocalSeqNumber;
import sun.util.resources.cldr.sn.LocaleNames_sn;

public class MySingleList implements IList {
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public void creatList() {
        ListNode node1 = new ListNode(11);
        ListNode node2 = new ListNode(22);
        ListNode node3 = new ListNode(33);
        ListNode node4 = new ListNode(44);
        ListNode node5 = new ListNode(55);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        head = node1;
    }
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
        } else {
            node.next = head;
            head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(head == null) {
            head = node;
        } else {
            while(cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        ListNode node  = new ListNode(data);
        if(index < 0 || index >= size()) {
            throw new InderException("index不合法");
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        else {
            ListNode cur =  head;
            ListNode curPre = cur;
            while( index != 0) {
                curPre = cur;
                cur = cur.next;
                index--;
            }
            curPre.next = node;
            node.next = cur;
        }
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;

    }
    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        while(cur != null) {
            count++;
            cur =  cur.next;
        }
        return  count;
    }

    @Override
    public void remove(int key) {
        if(head == null) {
            return;
        }
        if(head.val == key) {
            head = head.next;
            return;
        }
        ListNode node = new ListNode(key);
        ListNode cur = head.next;
        ListNode curPre = head;
        while(cur != null) {
            if(cur.val == key) {
                curPre.next = cur.next;
                return;
            }
            curPre = curPre.next;
            cur = cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        if(head == null) {
            return ;
        }
        ListNode curPre = head;
        ListNode cur = head.next;
        while(cur != null) {
            if(cur.val == key) {
                curPre.next = cur.next;
                cur = cur.next;
            }
            else {
                cur = cur.next;
                curPre = curPre.next;
            }
        }
        if(head.val == key) {
            head = head.next;
        }
    }





    @Override
    public void clear() {
        head = null;

    }

    @Override
    public void display() {
        if(head == null) {
            return ;
        }
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

}
